﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OMailLineMacTypeReport : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.导出 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OMailLineMacTypeReport";
            }
        }
        #region 属性 (查询条件)

        protected int Year
        {
            get
            {
                return GetRequestValue<int>("year", DateTime.Now.Year);
            }
        }
        protected int Month
        {
            get
            {
                return GetRequestValue<int>("Month", 1);
            }
        }

        protected string colMonth
        {
            get
            {
                return Year.ToString() + "-" + Month.ToString();
            }
        }
        protected DateTime stated
        {
            get
            {
                return Convert.ToDateTime(colMonth);
            }
        }

        protected DateTime endd
        {
            get
            {
                return stated.AddYears(1).AddDays(-1);
            }
        }

        protected int showchart
        {
            get
            {
                return GetRequestValue<int>("showchart", 0);
            }
        }


        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //DataBind();


            AddLog(Permission.查看);
        }
        /// <summary>
        /// 当前排序字段
        /// </summary>
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "MachineType" : base.orderField;
            }
        }
        /// <summary>
        /// 当前排序方式
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "Desc" : base.orderDirection;
            }
        }
        protected void DataBind()
        {
            DataTable DTC = OMailLineController.OMailLine_GetMacTypeReport(stated.ToString("yyyy-MM-dd"), endd.ToString("yyyy-MM-dd 23:59:59"));
            //DTC.Columns[0].Caption = "机型";

            DTC.DefaultView.Sort = SQLSort;

           // this.reportGV.DataSource = DTC.DefaultView;
           // this.reportGV.DataBind();
          //  this.reportGV.Columns[0].HeaderText = "机型";

        }
        protected DataTable _GetDT = null;
        protected DataTable GetDT
        {
            get
            {
                if (_GetDT == null)
                {
                    DataTable DT =   OMailLineController.OMailLine_GetMacTypeReport(stated.ToString("yyyy-MM-dd"), endd.ToString("yyyy-MM-dd 23:59:59"));
                    DT.DefaultView.Sort = SQLSort;
                   // _GetDT = DT.Copy();
                    _GetDT = DT.DefaultView.ToTable();
                }
                return _GetDT;
            }
        }
        /// <summary>
        /// 获取表格HTML
        /// </summary>
        /// <returns></returns>
        protected string GetTableHtml(int layouth)
        {
            StringBuilder sb = new StringBuilder();
            //1.头部
            sb.AppendFormat("<table class=\"table\" width=\"100%\" layouth=\"{0}\">", layouth);
            sb.Append("<thead><tr  >");
            foreach (DataColumn dc in GetDT.Columns)
            {
                sb.AppendFormat("<th {1}>{0}</th>", dc.ColumnName == "MachineType" ? "机型" : dc.ColumnName, GetDataListOrder(dc.ColumnName));
            }

            sb.Append("</tr></thead><tbody>");
            //2.行
            foreach (DataRow dr in GetDT.Rows)
            {
                sb.AppendLine("<tr>");
                foreach (DataColumn dc in GetDT.Columns)
                {
                     sb.AppendFormat("<td>{0}</td>", dr[dc.ColumnName]);
                }
                sb.AppendLine("</tr>");
            }

            sb.Append("</tbody></table>");
            return sb.ToString();
        }

        protected string GetYearList()
        {
            string liTemplete = "<option value='{0}' {1}>{0}年</option>";
            string rHtml = "";
            int nowYear = DateTime.Now.Year;
            for (int i = (nowYear - 3); i <= nowYear; i++)
            {
                rHtml = rHtml + string.Format(liTemplete, i, i == Year ? "selected" : "");
            }
            return rHtml;
        }

        /// <summary>
        /// 获取报表2 主表的X轴
        /// </summary>
        /// <returns></returns>
        protected string GetReport2JSCategories()
        {
            DataTable dtcl = GetDT;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int rowindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "MachineType")
                {
                    rowindex++;
                    continue;
                }
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (rowindex != dtcl.Columns.Count)
                {
                    sb.AppendFormat(",");
                }
                rowindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport2JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = GetDT;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");


            int rowNum = 1;

            foreach (DataRow dr in dtmain.Rows)
            {
                string str直接 = "{name: '" + dr["MachineType"] + "',data:[";
                int cNum = 1;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "MachineType")
                        continue;
                    str直接 += dr[dc.ColumnName];
                    if (cNum != dtmain.Columns.Count - 1)
                    {
                        str直接 += ",";
                    }
                    cNum++;
                }
                str直接 += "]}";

                if (rowNum != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }

                sb.AppendLine(str直接);
                rowNum++;
            }



            sb.AppendLine("]");
            return sb.ToString();
        }
    }
}